package com.westcom.vulb.logic;

import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.westcom.vulb.network.http.VulbHttp;
import com.westcom.vulb.protocal.http.Response;

public class card extends BasicLogic 
{
	private static String CreateCard_url = base_url+"web/create_card";
	private static String CopyCard_url = base_url+"web/copy_card";
	private static String UpdateCardContent_url = base_url+"web/update_card";
	private static String GetCardList_url = base_url+"web/card_list";
	private static String GetMostGoodCard_url = base_url+"web/get_most_good_card";
	private static String CardSendByMail_url = base_url+"web/card_list";
	private static String GetCardViewedUserList_url = base_url+"web/card_referenced_member_list";
	private static String Valuat_url = base_url+"web/card_valuation";
	private static String GetCardGoodUserList_url = base_url+"web/card_good_member_list";
	private static String StartEditCard_url = base_url+"web/start_edit_card";
	private static String EndEditCard_url = base_url+"web/end_edit_card";
	private static String ReferenceCard_url = base_url+"web/reference_card";

		
		
	public static card getInstanse()
	{
		return new card();
	}
	public Response CreateCard(String sessionId,int categoryId,String contents,int[] resourceIds)
	{
		JSONObject j = new JSONObject();
		try 
		{
			j.put("categoryId", categoryId);
			j.put("contents", contents);
			j.put("resourceIds", intArray2JSONArray(resourceIds));
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		builder.addTextBody("params", j.toString());
		builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
		Response response = VulbHttp.post(CreateCard_url,buildHeader(sessionId),builder.build());
		return response;		 
	}
	public Response CopyCard(String sessionId,int categoryId,int[]cardIds)
	{   		  
	    JSONObject j = new JSONObject();
		try 
		{
			j.put("categoryId", categoryId);
			j.put("cardIds", intArray2JSONArray(cardIds));
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		builder.addTextBody("params", j.toString());
		builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
		Response response = VulbHttp.post(CopyCard_url,buildHeader(sessionId),builder.build());
		return response;
	}
	
	public Response StartEditCard(String sessionId,int cardId)
	{
		JSONObject j = new JSONObject();
		try 
		{
			j.put("cardId", cardId);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		builder.addTextBody("params", j.toString());
		builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
		Response response = VulbHttp.post(StartEditCard_url,buildHeader(sessionId),builder.build());
		return response;
	}
	public Response UpdateCardContent(String sessionId,int cardId,String contents,int[]resourceIds)
	{				   
		JSONObject j = new JSONObject();
		try 
		{
			j.put("cardId", cardId);
			j.put("contents", contents);
			j.put("resourceIds", intArray2JSONArray(resourceIds));
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		builder.addTextBody("jsessionid",sessionId);
		builder.addTextBody("params",j.toString());
		builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
		Response response = VulbHttp.post(UpdateCardContent_url,buildHeader(sessionId),builder.build());
		return response;

	}
	public Response EndCardEdit(String sessionId,int cardId)
	{
		JSONObject j = new JSONObject();
		try 
		{
			j.put("cardId", cardId);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		builder.addTextBody("params", j.toString());
		builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
		Response response = VulbHttp.post(EndEditCard_url,buildHeader(sessionId),builder.build());
		return response;
	}
	public Response GetCardList(String sessionId,JSONArray categories)
	{            		  
		JSONObject j = new JSONObject();
		try 
		{
			j.put("categories",categories);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		builder.addTextBody("params", j.toString());
		builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
		Response response = VulbHttp.post(GetCardList_url,buildHeader(sessionId),builder.build());
		return response;
	}
	public Response GetMostGoodCard(String sessionId,int sheet_id,int count)
	{
	    JSONObject j = new JSONObject();
		try 
		{
			j.put("sheet_id", sheet_id);
			j.put("count", count);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		builder.addTextBody("params", j.toString());
		builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
		Response response = VulbHttp.post(GetMostGoodCard_url,buildHeader(sessionId),builder.build());
		return response;
	}
	public Response CardSendByMail(String sessionId,String mailType/*"newCard","updateCard" */,int cardId,int categoryId,int[] cardIds)
	{	    		
		JSONObject j = new JSONObject();
		try 
		{
			j.put("mailType", mailType);
			j.put("cardId", cardId);
			j.put("categoryId", categoryId);
			j.put("cardIds", intArray2JSONArray(cardIds));
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		builder.addTextBody("params", j.toString());
		builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
		Response response = VulbHttp.post(CardSendByMail_url,buildHeader(sessionId),builder.build());
		return response;

	}
	public Response GetCardViewedUserList(String sessionId,int cardId)
	{
		JSONObject j = new JSONObject();
		try 
		{
			j.put("cardId", cardId);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		builder.addTextBody("params", j.toString());
		builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
		Response response = VulbHttp.post(GetCardViewedUserList_url,buildHeader(sessionId),builder.build());
		return response;
	}
	public Response Valuat(String sessionId,int cardId,int valuation)// 0: none , 1:good , 2:bad ( unused )
	{
		JSONObject j = new JSONObject();
		try 
		{
			j.put("cardId", cardId);
			j.put("valuation", ""+valuation);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		builder.addTextBody("params", j.toString());
		builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
		Response response = VulbHttp.post(Valuat_url,buildHeader(sessionId),builder.build());
		return response;

	}
	public Response GetCardGoodUserList(String sessionId,int cardId)
	{
		JSONObject j = new JSONObject();
		try 
		{
			j.put("cardId", cardId);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		builder.addTextBody("params", j.toString());
		builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
		Response response = VulbHttp.post(GetCardGoodUserList_url,buildHeader(sessionId),builder.build());
		return response;
	}
	public Response ReferenceCard(String sessionId,int cardId)
	{
		JSONObject j = new JSONObject();
		try 
		{
			j.put("cardId", cardId);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		builder.addTextBody("params", j.toString());
		builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
		Response response = VulbHttp.post(ReferenceCard_url,buildHeader(sessionId),builder.build());
		return response;
	}
}
